package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqydjlbBaDetail;
import com.ruoyi.report_lq.domain.LqzrdjlbBaDetail;
import com.ruoyi.report_lq.domain.bo.LqydjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.LqydjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBaDetailVo;
import com.ruoyi.report_lq.mapper.LqydjlbBaDetailMapper;
import com.ruoyi.report_lq.service.ILqydjlbBaDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青延度记录Service业务层处理
 *
 * @author zyb
 * @date 2024-06-11
 */
@RequiredArgsConstructor
@Service
public class LqydjlbBaDetailServiceImpl implements ILqydjlbBaDetailService {

    private final LqydjlbBaDetailMapper baseMapper;

    /**
     * 查询沥青延度记录
     */
    @Override
    public LqydjlbBaDetailVo queryById(Long liqydid){
        return baseMapper.selectVoById(liqydid);
    }

    /**
     *
     * @param infoId
     * @return 针入度集合
     * @method 根据info-id 查询针入度子表的list集合数据
     */
    public List<LqydjlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<LqydjlbBaDetail> query = new QueryWrapper<>();
        query.eq("lqzrdjlbid", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询沥青延度记录列表
     */
    @Override
    public TableDataInfo<LqydjlbBaDetailVo> queryPageList(LqydjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqydjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<LqydjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青延度记录列表
     */
    @Override
    public List<LqydjlbBaDetailVo> queryList(LqydjlbBaDetailBo bo) {
        LambdaQueryWrapper<LqydjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqydjlbBaDetail> buildQueryWrapper(LqydjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqydjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLqzrdjlbid() != null, LqydjlbBaDetail::getLqzrdjlbid, bo.getLqzrdjlbid());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), LqydjlbBaDetail::getShiybh, bo.getShiybh());
        lqw.eq(bo.getShiywd() != null, LqydjlbBaDetail::getShiywd, bo.getShiywd());
        lqw.eq(bo.getShij1cz() != null, LqydjlbBaDetail::getShij1cz, bo.getShij1cz());
        lqw.eq(bo.getShij2cz() != null, LqydjlbBaDetail::getShij2cz, bo.getShij2cz());
        lqw.eq(bo.getShij3cz() != null, LqydjlbBaDetail::getShij3cz, bo.getShij3cz());
        lqw.eq(bo.getYanssd() != null, LqydjlbBaDetail::getYanssd, bo.getYanssd());
        lqw.eq(bo.getPingjz() != null, LqydjlbBaDetail::getPingjz, bo.getPingjz());
        return lqw;
    }

    /**
     * 新增沥青延度记录
     */
    @Override
    public Boolean insertByBo(LqydjlbBaDetailBo bo) {
        LqydjlbBaDetail add = BeanUtil.toBean(bo, LqydjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青延度记录
     */
    @Override
    public Boolean updateByBo(LqydjlbBaDetailBo bo) {
        LqydjlbBaDetail update = BeanUtil.toBean(bo, LqydjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增沥青延度记录子
     */
    public Boolean insertOrUpdateByZrdBo(List<LqydjlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            LqydjlbBaDetail detail = new LqydjlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setShiybh(detailVo.getShiybh());
            detail.setShiywd(detailVo.getShiywd());
            detail.setYanssd(detailVo.getYanssd());
            detail.setShij1cz(detailVo.getShij1cz());
            detail.setShij2cz(detailVo.getShij2cz());
            detail.setShij3cz(detailVo.getShij3cz());
            detail.setPingjz(detailVo.getPingjz());
            detail.setLqzrdjlbid(pId);
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqydjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青延度记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
